package com.fh.wanandroid.ui.articledetail;

import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.appcompat.widget.Toolbar;

import com.fh.wanandroid.R;
import com.fh.wanandroid.base.BaseActivity;
import com.fh.wanandroid.base.BasePresenter;
import com.fh.wanandroid.utils.CommonUtils;
import com.fh.wanandroid.utils.StatusBarUtil;
import com.just.agentweb.AgentWeb;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ArticleDetailActivity extends BaseActivity {

    @BindView(R.id.article_detail_toolbar)
    Toolbar articleDetailToolbar;
    @BindView(R.id.article_detail_web_view)
    FrameLayout articleDetailWebView;
    @BindView(R.id.article_detail_group)
    LinearLayout articleDetailGroup;

    private Bundle bundle;
    private int id;
    private String title;
    private String articleLink;
//    private St

    private AgentWeb mAgentWeb;




    @Override
    protected void onPause() {
//        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
//        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }
    @Override
    public void onDestroy() {
//        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }


    @Override
    public BasePresenter getmPresenter() {
        return null;
    }

    @Override
    public void presenterSetView() {

    }

    @Override
    public void initView() {

//      articleDetailToolbar.setTitle(Html.fromHtml(title));

    }

    @Override
    public void initData() {

        getBundleData();

        articleDetailToolbar.setTitle(Html.fromHtml(title));
        setSupportActionBar(articleDetailToolbar);
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, articleDetailToolbar);

        articleDetailToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initEventAndData();

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_article_detail;
    }


    protected void initEventAndData() {

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(articleDetailWebView, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setMainFrameErrorView(R.layout.webview_error_view, -1)
                .createAgentWeb()
                .ready()
                .go(articleLink);

        WebView mWebView = mAgentWeb.getWebCreator().getWebView();
        WebSettings mSettings = mWebView.getSettings();
//        if (mPresenter.getNoImageState()) {
//            mSettings.setBlockNetworkImage(true);
//        } else {
            mSettings.setBlockNetworkImage(false);
//        }
        mSettings.setAppCacheEnabled(true);
        mSettings.setDomStorageEnabled(true);
        mSettings.setDatabaseEnabled(true);

//        if (mPresenter.getAutoCacheState()) {
            mSettings.setAppCacheEnabled(true);
            mSettings.setDomStorageEnabled(true);
            mSettings.setDatabaseEnabled(true);
            if (CommonUtils.isNetworkConnected()) {
                mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
            } else {
                mSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            }
//        } else {
//            mSettings.setAppCacheEnabled(false);
//            mSettings.setDomStorageEnabled(false);
//            mSettings.setDatabaseEnabled(false);
//            mSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        }

        mSettings.setJavaScriptEnabled(true);
        mSettings.setSupportZoom(true);
        mSettings.setBuiltInZoomControls(true);
        //不显示缩放按钮
        mSettings.setDisplayZoomControls(false);
        //设置自适应屏幕，两者合用
        //将图片调整到适合WebView的大小
        mSettings.setUseWideViewPort(true);
        //缩放至屏幕的大小
        mSettings.setLoadWithOverviewMode(true);
        //自适应屏幕
        mSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }
    public void  getBundleData(){

        bundle = getIntent().getExtras();
        title= bundle.getString("title");
        id = bundle.getInt("id");
        articleLink = bundle.getString("link");
        Log.e("articledetail","-------------title:"+title+",--------id:"+id);

    }
}
